home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1393 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: What the hell is THIS?!
  5. Date: Sat, 13 Jan 96 13:27:25 GMT
  6. Organization: none
  7. Message-ID: <821539645snz@genesis.demon.co.uk>
  8. References: <4d6rgh$rfu@abel.cc.sunysb.edu> <coc-1301960253420001@dal1498.computek.net>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <coc-1301960253420001@dal1498.computek.net>
  15.            coc@computek.net "Chad Cranfill" writes:
  16.  
  17. >In article <4d6rgh$rfu@abel.cc.sunysb.edu>, bmadhusu@engws12.ic.sunysb.edu
  18. >(Bommasamudram Madhusudan) wrote:
  19. >
  20. >> Can someone explain what
  21. >> 
  22. >>   int (*p)[3]  is?????
  23. >> 
  24. >
  25. >Starting with the "p", we parse the expression thusly: "p is an array of 3
  26. >pointers to int".
  27.  
  28. No, it is a pointer to an array of 3 ints.
  29.  
  30. You read a declaration from the 'inside out' i.e. you read the parts
  31. that are bound more closely to the identifier first. In this case the
  32. parentheses group the * with p so p is a pointer. OTOH int *q[3] declares an
  33. array of 3 pointers to int because [], and also (), bind more tightly than *.
  34. This is equivalent behaviour to expressions where [] and () are often said to
  35. have higher precedence than *.
  36.  
  37. >If you need help with this (believe me, I did!) get the
  38. >book "Deep C Secrets".
  39.  
  40. I think you'd better reread it! :-)
  41.  
  42. >> 
  43. >> I can say things like:
  44. >> 
  45. >> (*p)[0] = 3; for e.g, but when I print the value using:
  46. >> 
  47. >>  printf("%d",(*p)[0]) I get a core dump!
  48.  
  49. Unless you've initialised p to point to something, dereferencing it results
  50. in undefined behaviour. This is true for any type of pointer.
  51.  
  52. >Instead of using the dereference operator here, you may want to just say:
  53. >
  54. >printf("%d", p[0]);
  55.  
  56. No, p[0] is equivalent to *p and evaluates to an array. The value of an array
  57. is a pointer to its first element so this is passing an int * value to a
  58. function expecting an int value, i.e. undefined behaviour.
  59.  
  60. -- 
  61. -----------------------------------------
  62. Lawrence Kirby | fred@genesis.demon.co.uk
  63. Wilts, England | 70734.126@compuserve.com
  64. -----------------------------------------
  65.